package com.ruoyi.cloudpc.sms;

import com.ruoyi.common.constant.SmsConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.sms.SendSmsEnum;
import com.ruoyi.common.sms.SendSmsUtil;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 短信服务
 * @author Ten
 */
@RestController
@RequestMapping("/cloudpc/sms")
public class SmsController extends BaseController {


    @Autowired
    private RedisCache redisCache;

    @GetMapping(value = "/login")
    public AjaxResult login(@RequestParam String phone) {
//        String phone = SecurityUtils.getUsername();
        SendSmsEnum sendSmsEnum = SendSmsEnum.verificationCode(SendSmsEnum.VERIFICATION_CODE_LOGIN);
        SendSmsUtil.sendSms(sendSmsEnum, phone);

        redisCache.setCacheObject(SmsConstants.VERIFICATION_CODE + SmsConstants.LOGIN + phone, sendSmsEnum.getTemplateParamSet()[0], 2, TimeUnit.MINUTES);
        return AjaxResult.success();
    }
    @GetMapping(value = "/pwd")
    public AjaxResult pwd(@RequestParam String phone) {
//        String phone = SecurityUtils.getUsername();
        SendSmsEnum sendSmsEnum = SendSmsEnum.verificationCode(SendSmsEnum.VERIFICATION_CODE_PWD);
        SendSmsUtil.sendSms(sendSmsEnum, phone);

        redisCache.setCacheObject(SmsConstants.VERIFICATION_CODE + SmsConstants.PWD + phone, sendSmsEnum.getTemplateParamSet()[0], 2, TimeUnit.MINUTES);
        return AjaxResult.success();
    }
    @GetMapping(value = "/regist")
    public AjaxResult regist(@RequestParam String phone) {
//        String phone = SecurityUtils.getUsername();
        SendSmsEnum sendSmsEnum = SendSmsEnum.verificationCode(SendSmsEnum.VERIFICATION_CODE_REGIST);
        SendSmsUtil.sendSms(sendSmsEnum, phone);

        redisCache.setCacheObject(SmsConstants.VERIFICATION_CODE + SmsConstants.REGIST + phone, sendSmsEnum.getTemplateParamSet()[0], 2, TimeUnit.MINUTES);
        return AjaxResult.success();
    }

    //  校验接口
    @GetMapping(value = "/validlogin")
    public AjaxResult validlogin(@RequestParam String code) {
        String phone = SecurityUtils.getUsername();

        String redisCode = redisCache.getCacheObject(SmsConstants.VERIFICATION_CODE + SmsConstants.LOGIN + phone);
        if (!code.equals(redisCode)) {
            return AjaxResult.error("验证码错误");
        }
        return AjaxResult.success();
    }
    @GetMapping(value = "/validpwd")
    public AjaxResult validpwd(@RequestParam String code) {
        String phone = SecurityUtils.getUsername();

        String redisCode = redisCache.getCacheObject(SmsConstants.VERIFICATION_CODE + SmsConstants.PWD + phone);
        if (!code.equals(redisCode)) {
            return AjaxResult.error("验证码错误");
        }
        return AjaxResult.success();
    }
    @GetMapping(value = "/validregist")
    public AjaxResult validregist(@RequestParam String code) {
        String phone = SecurityUtils.getUsername();

        String redisCode = redisCache.getCacheObject(SmsConstants.VERIFICATION_CODE + SmsConstants.REGIST + phone);
        if (!code.equals(redisCode)) {
            return AjaxResult.error("验证码错误");
        }
        return AjaxResult.success();
    }

}
